package com.yanqu.road.server.manager.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.tradewar.*;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.tradewar.TradeWarActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.sort.TradeWarWantedSort;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 */
public class CrossTradeWarActivityGroup {

    private final Object lock = new Object();
    // 活动
    private Map<Integer, CrossTradeWarActivity> groupMap = new ConcurrentHashMap<>();

    //配置
    private ActivityInfo activityInfo;

    //所有分组的数据
    private static LinkedList<CrossTradeWarNotice> noticeListFromDb = new LinkedList<>();
    private static TradeWarWantedSort tradeWarWantedSort = new TradeWarWantedSort();
    private static Map<String, TradeWarWanted> crossTradeWarWantedMapFromDb = new ConcurrentHashMap<>();        // 所有通缉
    private static Map<String, List<TradeWarTrackRecord>> tradeWarTrackRecordMapFromDb = new ConcurrentHashMap<>();           //uid 通缉记录


    public CrossTradeWarActivityGroup(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
        loadDbData();
    }

    public boolean loadDbData() {
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        LinkedList<CrossTradeWarNotice> tempNoticeList = TradeWarActivityBussiness.getCrossTradeWarNoticeList(activityInfo.getActivityId(), zeroTime / 1000);
        noticeListFromDb = tempNoticeList;
        Map<String, TradeWarWanted> tempTradeWarWantedMap = TradeWarActivityBussiness.getTradeWarWantedMap(activityInfo.getActivityId());
        for (TradeWarWanted data : tempTradeWarWantedMap.values()) {
            if (data.getUseItemId() == 0) {
                data.setUseItemId(GameConfig.TRADE_WAR_WANTED_ITEM_ID);
            }
        }
        crossTradeWarWantedMapFromDb = tempTradeWarWantedMap;
        Map<String, List<TradeWarTrackRecord>> tempTradeWarTrackRecordMapFromDb = TradeWarActivityBussiness.getTradeWarTrackRecordMapByActivityId(activityInfo.getActivityId());
        tradeWarTrackRecordMapFromDb = tempTradeWarTrackRecordMapFromDb;
        return true;
    }


    public CrossTradeWarActivity getActivity(int activityId,int groupId) {

        if (!groupMap.containsKey(groupId)) {
            synchronized (lock) {
                if (!groupMap.containsKey(groupId)) {
                    UnionActivityGroup groupData = Cross2UnionActivityGroupMgr.getGroupData(activityId, groupId);
                    CrossTradeWarActivity activity = new CrossTradeWarActivity(activityInfo,this,groupId,groupData);
                    activity.initData(noticeListFromDb,crossTradeWarWantedMapFromDb,tradeWarTrackRecordMapFromDb);
                    groupMap.put(groupId, activity);
                }
            }
        }

        return groupMap.get(groupId);
    }

    public Map<Integer, CrossTradeWarActivity> getGroupMap(){
        return groupMap;
    }

}
